import {h5Login} from "@/wlkutils/wechat/h5";
import share from "@/wlkutils/share";
import platform from "@/wlkutils/platform";
import { toUrl } from '@/wlkutils/tools';
export const state = {
    token: uni.getStorageSync("token") || "",
    isLogin: uni.getStorageSync("isLogin") || false,
    userInfo: uni.getStorageSync("userInfo") || {},
    loginPopupShow: false,//控制是否打开登录弹窗
    showWxLogin: false,//微信授权登录弹窗
    loadingShow: false,//加载动画
};

export const getters = {
    token: (state) => state.token,
    isLogin: (state) => state.isLogin,
    userInfo: (state) => state.userInfo,
    loginPopupShow: state => state.loginPopupShow,
    showWxLogin: state => state.showWxLogin,
    loadingShow: state => state.loadingShow,
};

export const actions = {
    async getUserInfo({commit, dispatch, getters}, token = "") {
        commit("isLogin", true);
        if (token) commit("token", token);
        const {code, data} = await uni.$u.http.post("user/index").catch((error) => {
            dispatch("logout");
            return false;
        });
        commit("userInfo", data);
        let agent_id = uni.getStorageSync('agent_id');
        if(!uni.$u.test.isEmpty(agent_id)){
            uni.$u.http.post("share/bindAgent",{
                agent_id:agent_id
            });
            uni.removeStorageSync('agent_id');
        }
        share.setShareInfo();
        return data;

    },
    async logout({commit, dispatch},toPage = false) {
        if (uni.getStorageSync("token")) {
            await uni.$u.http.post("user/logout");
        }
        uni.removeStorageSync('token');
        commit("token", "");
        commit("isLogin", false);
        commit("userInfo", {});
        if(toPage){
            toUrl('/pages/index/user');
        }
        share.setShareInfo();
    },
    // 自动静默登录----获取code
    async getLoginByUnion({commit, dispatch, getters}) {
        console.log('自动静默登录')
        if (!getters.isLogin) {
            if (getters.token && platform.get() == 'wxMiniProgram') {
                uni.login({
                    success(res) {
                        uni.$u.http.post('user/mpLogin', {wxCode: res.code},{custom: {hideMsg: true}}).then(res => {
                            dispatch("getUserInfo", res.data.token);
                        }).catch(() => {
                            // 自动静默登录失败
                            console.log('自动静默登录失败')
                        })
                    },
                    fail(res) {
                        console.log(res)
                    }

                });
            } else if (platform.get() == 'wxOfficialAccount') {
                //公众号自动登录 开启后可能导致进入快照模式
                // h5Login();
                share.setShareInfo();
            } else if (platform.get() == 'H5') {
                share.setShareInfo();
            }
        } else {
            await dispatch("getUserInfo");
        }
    },

};

export const mutations = {
    token(state, payload) {
        state.token = payload;
        uni.setStorageSync("token", payload);
    },

    isLogin(state, data) {
        state.isLogin = data;
        uni.setStorageSync("isLogin", data);
    },

    userInfo(state, data) {
        state.userInfo = data;
        uni.setStorageSync("userInfo", data);
    },
    setLoginPopupShow(state, data) {
        state.loginPopupShow = data;
    },
    setShowWxLogin(state, data) {
        state.showWxLogin = data;
    },
    setLoadingShow(state, data) {
        state.loadingShow = data;
    },

};
